import Dialog from './dialog.vue'
import Upload from '../upload'
import i18n from '@/locale'

// 定义插件对象
const MessageBox = {}
// vue的install方法，用于定义vue插件
MessageBox.install = function (Vue, options) {
  const MessageBoxInstance = Vue.extend(Dialog)
  let currentMsg
  const initInstance = ({content, parent, params, ok, cancel}) => {
    // 实例化vue实例
    currentMsg = new MessageBoxInstance({
      i18n,
      mixins: [{
        methods: {
          okx (vm) {
            if (typeof ok === 'function') {
              ok(vm.$refs.mldContent)
            }
          },
          cancel (vm) {
            if (typeof cancel === 'function') {
              cancel(vm.$refs.mldContent)
            }
          }
        }
      }],
      components: {
        MldContent: content
      },
      data () {
        return {
          parent: parent,
          params: params
        }
      }
    })
    let msgBoxEl = currentMsg.$mount().$el
    document.body.appendChild(msgBoxEl)
  }
  // 在Vue的原型上添加实例方法，以全局调用
  Vue.prototype.$msgBox = {
    showUploadDialog (options) {
      if (!options.title) {
        options.title = '选择图片'
      }
      options.content = Upload
      this.showDialog(options)
    },
    showDialog (options) {
      // if (!currentMsg) {
      initInstance({content: options.content, parent: options.parent, params: options.params, ok: options.ok, cancel: options.cancel})
      // }
      if (typeof options === 'string') {
        // currentMsg.content = options
      } else if (typeof options === 'object') {
        Object.assign(currentMsg, options)
      }
      currentMsg.visible = true
      if (options.fullscreen === true) {
        currentMsg.fullscreen = true
      } else {
        currentMsg.fullscreen = false
      }
      if (options.draggable === true) {
        currentMsg.draggable = true
      }
      if (options.width) {
        currentMsg.width = options.width
      }
      if (options.height) {
        currentMsg.height = options.height
      }
      currentMsg.title = options.title
      if (options.showOk === false) {
        currentMsg.showOk = false
      }
      if (options.showCancel === false) {
        currentMsg.showCancel = false
      }
      if (options.minMax === false) {
        currentMsg.minMax = false
      }
      // return currentMsg.showMsgBox()
    }
  }
}
export default MessageBox
